package com.tevintao.marketingsystem.tool;

import android.os.Message;

import com.tevintao.marketingsystem.log.LogManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by tevintao-1 on 2016/7/14.
 */
public class DownloadFile {

    public static void downloadfile(String urlStr, String path) {
        File f = new File(path);
        if (f.exists()) {
            f.delete();
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            // 构造URL
            URL url = new URL(urlStr);
            // 打开连接
            URLConnection con = url.openConnection();
            // 输入流
            is = con.getInputStream();
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流
            os = new FileOutputStream(path);
            // 开始读取
            int i = 0;
            int arg = 0;
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
                i += len;

            }
            // 完毕，关闭所有链接
            os.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static boolean downLoad(String serverPath, String savedPath) {
        LogManager.getInstance().logInfo("serverPath = " + serverPath + " savePath = " + savedPath);
        long currentMill = System.currentTimeMillis();
        try {
            URL url = new URL(serverPath);
            HttpURLConnection _conn = (HttpURLConnection) url.openConnection();
            _conn.setRequestMethod("GET");
            _conn.setConnectTimeout(5000);
            int code = _conn.getResponseCode();
            if (code == 200) {
                //设置进度条的长度
                InputStream is = _conn.getInputStream();
                File _file = new File(savedPath);
                FileOutputStream _FileOutputStream = new FileOutputStream(_file);
                int len;
                byte[] buffer = new byte[1024];
                int _total = 0;
                while ((len = is.read(buffer)) != -1) {
                    _FileOutputStream.write(buffer, 0, len);
                    _total += len;
                    if (System.currentTimeMillis() - currentMill > 3 * 1000) {
                        LogManager.getInstance().logInfo("_total = " + _total);
                    }
                }
                _FileOutputStream.flush();
                _FileOutputStream.close();
                is.close();
                return true;
            } else {
                LogManager.getInstance().logInfo("code = " + code);
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogManager.getInstance().logError("下载文件错误 exception = " + e.toString());
            return false;
        }

    }



}
